package com.zeoy.keeplivetestdemo.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.zeoy.keeplivetestdemo.service.BackGroundService;
import com.zeoy.keeplivetestdemo.utils.ServiceCheckUtils;
import com.zeoy.keeplivetestdemo.app.MyApplication;
import com.zeoy.keeplivetestdemo.service.ForeGroundService;

/**
 * Created by Administrator on 2018\3\21 0021.
 */

public class StartReceiver extends BroadcastReceiver {
    private boolean isServiceRunning;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("zeoy", "【收到启动广播】");
        /*if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {//如果广播是每分钟发送一次的时间广播
            Log.e("timeBroad", "时间变化了");
            isServiceRunning = ServiceCheckUtils.isServiceWorked(MyApplication.getmContext(), "forceground");
            if (!isServiceRunning) {
                Intent i = new Intent(context, ForeGroundService.class);
                context.startService(i);
            }
        }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.e("timeBroad", "屏幕解锁了");
            isServiceRunning = ServiceCheckUtils.isServiceWorked(MyApplication.getmContext(), "forceground");
            if (!isServiceRunning) {
                Intent i = new Intent(context, ForeGroundService.class);
                context.startService(i);
            }
        }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.e("timeBroad", "屏幕关闭了");
            isServiceRunning = ServiceCheckUtils.isServiceWorked(MyApplication.getmContext(), "forceground");
            if (!isServiceRunning) {
                Intent i = new Intent(context, ForeGroundService.class);
                context.startService(i);
            }
        }*/
        isServiceRunning = ServiceCheckUtils.isServiceWorked(MyApplication.getmContext(),
                "com.zeoy.keeplivetestdemo.service.BackGroundService");
        if (!isServiceRunning) {
            Log.e("zeoy", "【广播    -  启动后台】");
            Intent i = new Intent(context, BackGroundService.class);
            context.startService(i);
        }
    }
}
